package com.zxbit.flash.ui.activity;

import android.annotation.SuppressLint;
import android.os.CountDownTimer;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.zxbit.flash.R;
import com.zxbit.flash.base.BaseTitleActivity;
import com.zxbit.flash.common.Constant;
import com.zxbit.flash.common.RouterMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 状态展示页
 */
@Route(name = "状态展示页", path = RouterMap.STATUS_DISPLAY)
public class StatusActivity extends BaseTitleActivity {

    @BindView(R.id.tvHint)
    TextView tvHint;
    @BindView(R.id.tvHint2)
    TextView tvHint2;
    @BindView(R.id.tv_next_step)
    TextView tvNextStep;

    @Autowired(name = Constant.PAGE_TYPE)
    int pageType; // 0:认证结果 1:借款结果
    private CountDownTimer countDownTimer;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onUIReady() {
        if (Constant.STATUS_TYPE_AUTH == pageType) {
            setTitleText(getStringResource(R.string.status_auth_page_title));
            tvHint.setText(R.string.status_auth_tip);
            tvNextStep.setText(getStringResource(R.string.status_jump_to_main));
        } else if (Constant.STATUS_TYPE_BORROW == pageType) {
            setTitleText(getStringResource(R.string.status_borrow_page_title));
            tvHint.setText(R.string.status_borrow_tip);
            tvNextStep.setText(getStringResource(R.string.status_jump_to_bill));
            countDownTimer = new CountDownTimer(1 * 1000, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    tvNextStep.setText(getStringResource(R.string.status_jump_to_bill));
                }

                @Override
                public void onFinish() {
                    jumpMain();
                }
            };
            countDownTimer.start();
        }
    }

    @OnClick(R.id.tv_next_step)
    public void onClick() {
        jumpMain();
    }

    private void jumpMain() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        switch (pageType) {
            case Constant.STATUS_TYPE_AUTH:
                ARouter.getInstance().build(RouterMap.LOANPAGE).navigation();
                break;
            case Constant.STATUS_TYPE_BORROW:
                ARouter.getInstance().build(RouterMap.MINE_BROWING).navigation();
                break;
            case Constant.STATUS_TYPE_ATM_REPAYMENT:
                ARouter.getInstance().build(RouterMap.LOANPAGE).navigation();
                break;
            case Constant.STATUS_TYPE_OTC_REPAYMENT:
                ARouter.getInstance().build(RouterMap.LOANPAGE).navigation();
                break;
            default:
                break;
        }
        finish();
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_result;
    }

    @Override
    public String getPageTitle() {
        if (Constant.STATUS_TYPE_AUTH == pageType ||Constant.STATUS_TYPE_BORROW == pageType) {
            return "";
        } else {
            return null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }
}
